package com.hgyc.mom.mes.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.mes.entity.MesWorkOrder;
import com.hgyc.mom.mes.vo.MesWorkOrderParam;
import com.hgyc.mom.mes.service.MesWorkOrderService;
import com.hgyc.mom.mes.vo.MesWorkOrderVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 生产工单Controller
 *
 * @author fwj
 * @since 2025-07-10
 */
@Tag(name = "生产工单控制器", description = "生产工单控制器")
@RestController
@RequestMapping("/mes/work/order")
public class MesWorkOrderController {
    @Autowired
    private MesWorkOrderService mesWorkOrderService;

    @Operation(summary = "查询生产工单列表", description = "查询生产工单列表")
    @PostMapping("/pageList")
    public Result<IPage<MesWorkOrder>> pageList(@Parameter(description = "查询参数") @RequestBody MesWorkOrderParam mesWorkOrderParam) {
        Page<MesWorkOrder> page = new Page<>(mesWorkOrderParam.getPage(), mesWorkOrderParam.getPageSize());
        MesWorkOrder mesWorkOrder = new MesWorkOrder();
        BeanUtils.copyProperties(mesWorkOrderParam, mesWorkOrder);
        QueryWrapper<MesWorkOrder> wrapper = QueryWrapperUtil.build(mesWorkOrder);
        IPage<MesWorkOrder> list = mesWorkOrderService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取生产工单详细信息", description = "获取生产工单详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "生产工单ID") @PathVariable("id") Long id) {
        return Result.success(mesWorkOrderService.getById(id));
    }
    @Operation(summary = "新增生产工单", description = "新增生产工单")
    @PostMapping
    public Result<?> save(@Parameter(description = "生产工单") @RequestBody MesWorkOrderVO mesWorkOrderVO) {
        mesWorkOrderService.saveOrder(mesWorkOrderVO);
        return Result.success();
    }

    @Operation(summary = "修改生产工单", description = "修改生产工单")
    @PutMapping
    public Result<?> update(@Parameter(description = "生产工单") @RequestBody MesWorkOrderVO mesWorkOrderVO) {
        mesWorkOrderService.updateOrder(mesWorkOrderVO);
        return Result.success();
    }

    @Operation(summary = "删除生产工单", description = "删除生产工单")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        mesWorkOrderService.removeOrder(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "工单拆分", description = "工单拆分")
    @PostMapping("/split")
    public Result<?> split(@RequestParam("id") Long id, @RequestParam("splitQty")  Integer splitQty) {
        mesWorkOrderService.split(id, splitQty);
        return Result.success();
    }

    @Operation(summary = "工单下达", description = "工单下达")
    @PutMapping("/transmit")
    public Result<?> transmit(@Parameter(description = "生产工单") @RequestBody MesWorkOrder mesWorkOrder) {
        mesWorkOrderService.transmit(mesWorkOrder);
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesWorkOrder> util = new ExcelUtil<>(MesWorkOrder.class);
        util.importTemplateExcel(response, "生产工单");
    }

    @Operation(summary = "导出生产工单列表", description = "导出生产工单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWorkOrderParam mesWorkOrderParam) {
        MesWorkOrder mesWorkOrder = new MesWorkOrder();
        BeanUtils.copyProperties(mesWorkOrderParam, mesWorkOrder);
        QueryWrapper<MesWorkOrder> wrapper = QueryWrapperUtil.build(mesWorkOrder);
        List<MesWorkOrder> list = mesWorkOrderService.list(wrapper);
        ExcelUtil<MesWorkOrder> util = new ExcelUtil<>(MesWorkOrder.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    @Operation(summary = "导入生产工单列表", description = "导入生产工单列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesWorkOrder> util = new ExcelUtil<>(MesWorkOrder.class);
        List<MesWorkOrder> mesWorkOrderList = util.importExcel(file.getInputStream());
        String message = mesWorkOrderService.importData(mesWorkOrderList, updateSupport);
        return Result.success(message);
    }
}
